//Load common code that includes config, then load the app logic for this page.
//将表单序列化成json格式的数据(但不适用于含有控件的表单，例如复选框、多选的select)
(function($) {
	$.fn.serializeJson = function() {
		var jsonData1 = {};
		var jd=[];
		var entity=null;
		var firstName="";
		var serializeArray = this.serializeArray();
		// 先转换成{"id": ["12","14"], "name": ["aaa","bbb"],
		// "pwd":["pwd1","pwd2"]}这种形式
		$(serializeArray).each(
				function(i) {
					if(i==0)
					{
						firstName=this.name;
					}
					if(firstName==this.name)
					{
						if(entity!=null)
						{
							jd.push(entity);
						}
						entity={};
					}
					entity[this.name]=this.value;
					if(i==serializeArray.length-1)
					{
						jd.push(entity);
					}
				});
				jsonData1=jd;
		// 再转成[{"id": "12", "name": "aaa", "pwd":"pwd1"},{"id": "14", "name":
		// "bb", "pwd":"pwd2"}]的形式
		var vCount = 0;
		// 计算json内部的数组最大长度
		for ( var item in jsonData1) {
			var tmp = $.isArray(jsonData1[item]) ? jsonData1[item].length : 1;
			vCount = (tmp > vCount) ? tmp : vCount;
		}

		if (vCount > 1) {
			var jsonData2 = new Array();
			for (var i = 0; i < vCount; i++) {
				var jsonObj = {};
				for ( var item in jsonData1) {
					jsonObj[item] = jsonData1[item][i];
				}
				jsonData2.push(jsonObj);
			}
			return JSON.stringify(jsonData2);
		} else {
			//return "[" + JSON.stringify(jsonData1) + "]";
			return JSON.stringify(jsonData1);
		}
	};
})(jQuery);

var submitSalesOrderResult=function(){	
	var actucalTimeNum=$("input[name=actualTime]");
	var quanDeliveryNum=$("input[name=quantityDelivered]");
	
	for(var i=0;quanDeliveryNum.length;i++)
	{
		
		if($(quanDeliveryNum[i]).val()!="")
		{
			if(isNaN($(quanDeliveryNum[i]).val()))
			{
				$("div.m-popup").css("display","block");
				return;
			}
		
			if($(actucalTimeNum[i]).val()==""||$(actucalTimeNum[i]).val()==undefined)
			{
				$("div.m-popup").css("display","block");
				return;
			}
		}	
	}	

	var jsonStr = $("#formPersonal").serializeJson();
	//alert(jsonStr);
	
	//var chk_value =[]; 
//	$('input[name="salesOrderIndex"]:checked').each(function(){ 
//	chk_value+=($(this).val())+",";
//	}); 
	//alert(chk_value);

	$.ajax({
		url : "/supplier/CreateOrderDelivery.htm?checkData=2",
		type : "POST",
		contentType : 'application/json;charset=utf-8', // 设置请求头信息
		dataType : "json",
		data : jsonStr,
		success : function(data) {
			window.location.href = "/supplier/orderList.htm";
		},
		error : function(res) {
			alert("提交失败，刷新页面重新提交");
			window.location.reload();
		}
	});
};


var submitConfirmOrder=function(id)
{
	var chk_value =[]; 
	$('input[name="purchaseOrderExecutionId"]:checked').each(function(){ 
	chk_value+=($(this).val())+",";
	}); 

	var jsonStr = $("#formPersonal").serializeJson();
	$.ajax({
		url : "/client/confirmOrder.htm?orderExecutionId="+chk_value,
		type : "POST",
		contentType : 'application/json;charset=utf-8', // 设置请求头信息
		dataType : "json",
		data : jsonStr,
		success : function(data) {
			window.location.href = "/client/purchaseOrderList.htm";
		},
		error : function(res) {
			alert("提交失败，刷新页面重新提交");
			window.location.reload();
		}
	});

};

var submitSupplyerAudit=function(obj)
{
	var vendorId=$("#vendorId").val();
	var purchaseOrderId=$("#purchaseOrderId").val();
	var status=obj;

	var grade="B";
	var detail=$("#detail").val();
	$.ajax({
		url : "/client/updateEstimateGrade.htm?vendorId="+vendorId+"&grade="+grade+"&dataContent="+detail,
		type : "POST",
		contentType : 'application/json;charset=utf-8', // 设置请求头信息
		dataType : "text",
		success : function(data) {
			window.location.href = "/client/nextPreSupplierAudit.htm?purchaseOrderID="+purchaseOrderId;
		},
		error : function(res) {
			alert("提交失败，刷新页面重新提交");
			window.location.reload();
		}
	});
};

var submitEstimate=function()
{
	var vendorId=$("#vendorId").val();
	//alert(vendorId);
	var status=$("#status").val();
	//alert(status);
	var grade=$("#fenshu2").text();
	if(grade=="1")
	{
		grade="E";
	}
	else if(grade=="2")
	{
		grade="D";
	}
	else if(grade=="3")
	{
		grade="C";
	}
	else if(grade=="4")
	{
		grade="B";
	}
	else if(grade=="5")
	{
		grade="A";
	}
	//alert(grade);
	var detail=$("#detail").val();
	//alert(detail);
	$.ajax({
		url : "/client/updateEstimateGrade.htm?vendorId="+vendorId+"&grade="+grade+"&status="+status+"&dataContent="+detail,
		type : "POST",
		contentType : 'application/json;charset=utf-8', // 设置请求头信息
		dataType : "text",
		data : detail,
		success : function(data) {
			window.location.href = "/client/supplierList.htm";
		},
		error : function(res) {
			alert("提交失败，刷新页面重新提交");
			window.location.reload();
		}
	});
};

var submitOrderEstimate=function()
{
	var purchaseOrderId=$("#purchaseOrderId").val();
	//var status=$("#status").val();
	//alert(status);
	var grade2=$("#fenshu2").text();
	var grade3=$("#fenshu3").text();
	var grade4=$("#fenshu4").text();
	var grade5=$("#fenshu5").text();
	var describe=$("#describe").val();
	$.ajax({
		url : "/client/updateOrderEstimate.htm?purchaseOrderId="+purchaseOrderId+"&grade2="+grade2+"&grade3="+grade3+"&grade4="+grade4+"&grade5="+grade5,
		type : "POST",
		contentType : 'application/json;charset=utf-8', // 设置请求头信息
		dataType : "text",
		data : describe,
		success : function(data) {
			window.location.href = "/client/purchaseOrderList.htm";
		},
		error : function(res) {
			alert("提交失败，刷新页面重新提交");
			window.location.reload();
		}
	});
};


var submitEstimateMessage=function()
{
	var name=prompt("请输入您的申诉","")
	{
		if(name!=null && name!="")
		{
			var receiveUserId=$("#receiveUserId").val();
			//alert(receiveUserId);
			
			var receiveEmail=$("#receiveEmail").val();
			//alert(receiveEmail);
			//alert(name);
			
			$.ajax({
					url : "/estimate/updateSendMessage.htm?receiveUserId="+receiveUserId+"&receiveEmail="+receiveEmail+"&msgContent="+name,
					type : "POST",
					contentType : 'application/json;charset=utf-8', // 设置请求头信息
					dataType : "text",
					//data : describe,
					success : function(data) {
						alert("申诉邮件已发送成功");
						//window.location.href = "/client/pipelineList.htm";
					},
					error : function(res) {
						alert("申诉邮件发送失败，请重新发送");
					//window.location.reload();
					}
			});		
		}
	}
};

var submitMessage=function()
{
	var name=prompt("请输入信息内容","")
	{
		if(name!=null && name!="")
		{
			var mobilePhone=$("#mobilePhone").val();			
			
			$.ajax({
					url : "/estimate/updateSendMsg.htm?mobilePhone="+mobilePhone+"&msgContent="+name,
					type : "POST",
					contentType : 'application/json;charset=utf-8', // 设置请求头信息
					dataType : "text",
					//data : describe,
					success : function(data) {
						alert("信息发送成功");
						//window.location.href = "/client/pipelineList.htm";
					},
					error : function(res) {
						alert("信息发送失败，请重新发送");
					//window.location.reload();
					}
			});		
		}
	}
};

function changeSupplyerOrder(obj,tdIndex){
          
          var selectValue = obj.options[obj.selectedIndex].value;
			$("#purchaseOrderTable").find("tr").each(function(i,e)
			{
				if(i>0)
				{
					if(selectValue!=0)
					{
						var tds=$(this).find("td");
						if(tds[tdIndex].innerText.trim()==selectValue)
						{
							$(this).show();
						}
						else
						{
							$(this).hide();
						}
					}
					else
					{
						$(this).show();
					}
				}
				
			});		
};

function changeSupplyerName()
{
	var selectValue = $("input[id='companyNameSelect']").val();
	$("#purchaseOrderTable").find("tr").each(function(i,e)
			{
				if(i>0)
				{
					if(selectValue!=0)
					{
						var tds=$(this).find("td");
						if((tds[1].innerText.trim().indexOf(selectValue))>0)
						{
							$(this).show();
						}
						else
						{
							$(this).hide();
						}
					}
					else
					{
						$(this).show();
					}
				}
				
			});
}

var submitReview=function()
{
	var chk_value =[]; 
	$('input[name="purchaseOrderIndex"]:checked').each(function(){ 
	chk_value+=($(this).val())+",";
	}); 
	
	var purchaseOrderID=$("#purchaseOrderIndex").val();
	
	window.location.href = "/client/preSupplierAudit.htm?purchaseOrderID="+purchaseOrderID+"&selectCount="+chk_value+"&currentPage=0";
	
}

var confirmAttachStatus=function(obj)
{
	var links=$("#"+obj);
	var paymentId=$("#paymentId").val();
	var isStatus1=true;
	var isReport=true;
	if(obj=="attachStatus1")
	{
		isReport=false;
		isStatus1=true;
		//alert(isStatus1);
	}
	else if(obj=="attachStatus2")
	{
		isReport=false;
		isStatus1=false;
		//alert(isStatus1);
	}
	else if(obj=="reportStatus1")
	{
		isReport=true;
		isStatus1=true;
		//alert(isStatus1);
	}
	else
	{
		isReport=true;
		isStatus1=false;
		//alert(isStatus1);
	}
	
	$.ajax({
					url : "/client/confirmAttachmentStatus.htm?paymentId="+paymentId+"&isStatus1="+isStatus1+"&isReport="+isReport,
					type : "POST",
					contentType : 'application/json;charset=utf-8', // 设置请求头信息
					dataType : "text",
					//data : describe,
					success : function(data) {
						//alert("申诉邮件已发送成功");
						changeTarget(links);
						//window.location.href = "/client/pipelineList.htm";
					},
					error : function(res) {
						alert("确认失败，请重新确认");
					}
			});	
};

var changeTarget=function(links)
{
 	//判断href属性是否包含“#”符号
    if ((links[0].getAttribute("href").indexOf("#")) >= 0) {
        var para = document.createElement("p");
        var fon = document.createElement("font");
        fon.setAttribute("color", "#808080");
        fon.innerHTML =links[0].lastChild.nodeValue;
        var content = para.appendChild(fon);
        replaceEach(links[0], content);
    } 
    else 
    {
        links[0].style.color = "990033";
    }
}

//替换HTML元素
var replaceEach=function(targetNode, newNode) {
    var targetParentNode = targetNode.parentNode;
    var newParentNode = newNode.parentNode;
    //若 targetParentNode 和 newParentNode 都存在父节点
    if (targetParentNode && newParentNode) {
        targetParentNode.replaceChild(newNode.cloneNode(true), targetNode);
    } else {
        newParentNode.replaceChild(targetNode, newNode);
    }
};

var changePurchaseOrder = function(obj) {
	var orderStatus = obj.options[obj.selectedIndex].value;
	
	window.location.href="/client/purchaseOrderList.htm?orderStatus="+orderStatus;
};


var changeOrder = function(obj) {
	var orderStatus = obj.options[obj.selectedIndex].value;
	//alert(orderStatus);
	
	window.location.href="/supplier/orderList.htm?orderStatus="+orderStatus;
	
	//$.ajax({
//		type : "POST",
//		contentType : 'application/json',
//		url : "/supplier/orderList.htm?orderStatus="+orderStatus,
//		success : function(data) {
//			//window.location.href = "/client/pipelineList.htm";
//		},
//		error : function(res) {
//			alert("提交失败，刷新页面重新提交");
//			window.location.reload();
//		}
//	});
};

var fileupLoad =function()
{
	var salesOrderID=$("#salesOrderID").val();
	var fileNameTxt=$("input[name=btnFile1]").val();
	var ss = fileNameTxt.split('\\');

	$("#fileName").text(ss[ss.length-1]);
	
	var deliverOrderNo=$("#deliverOrderNo").text();	
	//alert(deliverOrderNo);
	
	$.ajaxFileUpload({
		url:"/supplier/orderFileUpLoad.htm?salesOrderID="+ salesOrderID+"&deliverOrderNo="+deliverOrderNo,
		fileElementId:'btnFile1',
		type:"post",
		dataType:"text",
		success:function (data,status){
			$("#fileName").css("display","block");//显示
			$("#fileNameDelete").css("display","block");//显示
			$("#btnFileID").css("display","none");//隐藏
		},
		error:function (data , status , error){
		alert(e);
		},
		
		async:false
	});
};

var delLoadFile =function()
{
	$("#fileNameDelete").css("display","none");//隐藏
	$("#btnFileID").css("display","block");//显示
	$("#fileName").css("display","none");//隐藏
};

var backToSupplyerIndex=function()
{
	window.history.back(-1);
};

var FP_fileupLoad =function(tSalesOrderPaymentID)
{
	var temp='FPbtnFile'+tSalesOrderPaymentID;
	alert(temp);

	$.ajaxFileUpload({
		url:"/supplier/orderFileUpLoadFP.htm?tSalesOrderPaymentID="+ tSalesOrderPaymentID ,
		fileElementId:temp,
		type:"post",
		dataType:"text",
		
		async:false
	});
};

var BG_fileupLoad =function(tSalesOrderPaymentID)
{
	var temp='btnFileBG'+tSalesOrderPaymentID;
	alert(temp);

	$.ajaxFileUpload({
		url:"/supplier/orderFileUpLoadBG.htm?tSalesOrderPaymentID="+ tSalesOrderPaymentID ,
		fileElementId:temp,
		type:"post",
		dataType:"text",
		
		async:false
	});
};


